package com.woniuxy.yuekeuser.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yuekeuser.common.Result;
import com.woniuxy.yuekeuser.controller.Info.CampsiteCommentInfo;
import com.woniuxy.yuekeuser.info.UserInfo;
import com.woniuxy.yuekeuser.model.CampsiteComment;
import com.woniuxy.yuekeuser.model.User;
import com.woniuxy.yuekeuser.service.ICampsiteCommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Freedom
 * @since 2020-11-03
 */
@RestController
@Slf4j
public class CampsiteCommentController {

    @Autowired
    private ICampsiteCommentService iCampsiteCommentService;

    /**
     * @MethodName campsiteComment
     * @Description 用户评论打分，上传参数为，图片集合，营地id，评论内容和星级
     * @Author Cheney
     * @Date 2020/11/5 10:29
     **/
    @PostMapping("user/campsiteComment")
    public Result campsiteComment(@RequestBody CampsiteCommentInfo campsiteCommentInfo, HttpSession session){
        log.info("评论请求成功，参数为{}",campsiteCommentInfo);
        log.info("图片集合{}",campsiteCommentInfo.getFileItems().size());
        UserInfo user = (UserInfo) session.getAttribute("User");
        if(user == null){
            return Result.fail("操作失败！请先登录！");
        }
        int campsiteId = (int) session.getAttribute("campsiteId");
        System.out.println(campsiteId);
        campsiteCommentInfo.setCampsiteId(campsiteId);
        campsiteCommentInfo.setUserId(user.getUserId());

        return Result.success(iCampsiteCommentService.campsiteComment(campsiteCommentInfo));
    }

    /**
     * @MethodName uploadCommentImage
     * @Description 上传单张评论图片
     * @Author Cheney
     * @Date 2020/11/5 12:02
     **/
    @PostMapping("user/uploadCommentImage")
    public Result uploadCommentImage(MultipartFile file){
        log.info("单个文件上传请求！参数为{}",file);

        String path = UUID.randomUUID()+"_"+file.getOriginalFilename();

        if(!file.isEmpty()){
            try {
                file.transferTo(new File("d:/tmp/"+path));
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }

        }

        return Result.success("http://localhsot:8080/"+path);
    }

    /**
     * @MethodName loadCampsiteCommentById
     * @Description 根据营地id加载该营地的所有评论，并分页，size为5
     * @Author Cheney
     * @Date 2020/11/5 13:55
     **/
    @GetMapping("campsite/loadCampsiteCommentById")
    public Result loadCampsiteCommentById(@RequestParam(defaultValue = "1") int currentPage,
                                          @RequestParam(defaultValue = "5") int pageSize,HttpSession session){
        if(session.getAttribute("campsiteId")!=null){
            int campsiteId = (Integer) session.getAttribute("campsiteId");
            Page<CampsiteComment> page = new Page<>();
            page.setCurrent(currentPage);
            page.setSize(pageSize);
            return Result.success(iCampsiteCommentService.loadCampsiteCommentById(page,campsiteId));
        }
        return Result.fail("当前营地不存在,无法获取评论");
    }

}

